Skip to main content

Using the Communication Service

This is the MOST important file in our code. This handles communication between different kinds of layers of an extension like content script or background script or popup's content script. All the methods in this class is written to handle the slight differences in chrome and firefox's implementations so that we can use only one method and everything works as expected.

All the methods that are here are well tested and used in live production extensions. They are very well commented in the code as well so you shouldn't face any issues when using these.

detectBrowser method

The detectBrowser method is used to detect whether or not a browser is firefox or chromium based. So on firefox it should return Browser.Firefox and on Chrome or Edge or Opera or Brave it should return Browser.Chromium

executeBrowserScriptForFirefox method

The executeBrowserScriptForFirefox method accepts some code as string and it executes them in the current webpage.

# triggering this from a popup will show alert in the webpage.
executeBrowserScriptForFirefox(`alert("Hello World")`);

executeBrowserScriptForChrome method

The executeBrowserScriptForChrome method accepts a function as parameter and executes it in the current webpage.

# triggering this from a popup will show alert in the webpage.
await executeBrowserScriptForChrome(()=>{ alert("Hello World"); }, [])

showInBrowserConsole method

The showInBrowserConsole method is used mostly for debugging use cases while writing code to make sure that everything is working fine.

Calling this method with data should be visible from regular developer console

showInBrowserConsole("Hello World");